#!/bin/sh
# This script provides an ability to run a program stored on an HTTP server
# (it is used every time an object is created)

if [ $# -eq 0 ]; then
echo "Usage: popcobjrun <url> arguments...."
exit 1
fi

MY_POPC_PROXY=""
prog=""
drun=0

if [ -n "$POPC_PROXY" ]; then
MY_POPC_PROXY="-proxy=$POPC_PROXY"
fi

#parse arguments
for arg in $* ; do
    if [ "$arg" = "-drun" ]; then
      drun=1
    elif [ `echo "$arg"|grep "^\-batch="` ]; then
      export POPC_BATCH=`echo $arg|sed s/-batch=//`
    elif [ -z "$prog" ]; then
      prog=$arg
    else
      args="$args $arg"
    fi
done

if [ -z "$POPC_LOCATION" ]; then
  if [ "X$POPC_BATCH" = "Xsmscg" ]; then
    POPC_LOCATION=`dirname $0`/..
  else
    POPC_LOCATION=@CMAKE_INSTALL_PREFIX@
  fi
export POPC_LOCATION
fi
. $POPC_LOCATION/etc/popc-runtime-env.sh


if [ "$drun" = "1" ]; then
    echo "Exec: $POPC_JOB_EXEC $prog $args"
else
    #echo "exec this : $POPC_JOB_EXEC $prog $args"
    $POPC_JOB_EXEC $prog $args &
    RETVAL=$?
    exit $RETVAL
fi
